package com.duanjc.shiro.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duanjc.shiro.entity.TPerms;
import com.duanjc.shiro.entity.TUser;
import com.duanjc.shiro.mapper.TUserMapper;
import com.duanjc.shiro.service.TUserService;
import com.duanjc.shiro.utils.SaltUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author duanjiangcheng
 * @since 2022-11-29 03:58:52
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {


    @Autowired
    private TUserMapper tUserMapper;


    @Override
    public void register(TUser user) {
        //处理业务调用dao
        //1.生成随机盐
        String salt = SaltUtils.getSalt(8);
        //2.保存数据
        user.setSalt(salt);
        //3.明文密码进行MD5+salt+散列
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setPassword(md5Hash.toHex());
        this.save(user);
    }

    /**
     * description: 根据用户名查询角色 <br>
     * version: 1.0 <br>
     * date: 2022/11/29 16:21 <br>
     * author: duanjiangcheng <br>
     *
     * @param username username
     * @return com.duanjc.shiro.entity.TUser
     */
    @Override
    public TUser findRolesByUserName(String username) {
        return tUserMapper.findRolesByUserName(username);
    }

    @Override
    public List<TPerms> findPermsByRoleId(Integer roleId) {
        return tUserMapper.findPermsByRoleId(roleId);
    }

    @Override
    public List<TUser> all() {
        return tUserMapper.all();
    }
}
